package controllers

import (
	//	"log"
	"blog-admin/models"
	"fmt"

	"github.com/astaxie/beego"
)

type CatController struct {
	CommonController
}

func (this *CatController) List() {
	this.Data["IsCategory"] = true
	cats, _ := models.GetAllCats()
	this.Data["Categories"] = cats
	this.TplName = "cat_list.html"
}
func (this *CatController) Get() {
	this.Data["IsHome"] = true
	ck, _ := this.Ctx.Request.Cookie("uname")
	cookieuname := ck.Value
	//	if err != nil {
	//		cookieuname = ck.Value
	//	}
	this.Data["Uname"] = cookieuname

	act := this.Input().Get("act")

	switch act {
	case "add":
		name := this.Input().Get("title")
		if len(name) == 0 {
			break
		}
		err := models.AddCategory(name)
		if err != nil {
			beego.Error(err)
		}
		this.Redirect("/cat", 301)
		return
	case "del":
		id := this.Input().Get("id")
		if len(id) == 0 {
			break
		}
		err := models.DelCategory(id)
		if err != nil {
			beego.Error(err)
		}
		this.Redirect("/cat", 301)
		return
	}
	this.Data["IsCategory"] = true
	cats, _ := models.GetAllCats()
	fmt.Println(cats)
	this.Data["Categories"] = cats
	this.TplName = "cat_list.html"
}
